//
//  HelpMeBagApplyNowVC.m
//  MRBang
//
//  Created by 铅笔 on 16/5/9.
//  Copyright © 2016年 杭州易网新科技. All rights reserved.
//

#import "HelpMeBagApplyNowVC.h"
#import "HelpMeBagApplyFirstStepVC.h"
//#import "PickViewSelect.h"
#import "LoginViewController.h"//登录界面

@interface HelpMeBagApplyNowVC ()<pickViewStrDelegate,UITextFieldDelegate>
{
//    UIButton *button_money;
    UITextField *textField_money;
    UIButton *button_time;
    UIButton *button_applyNow;
    
    UILabel *label_circleFirst;
    UILabel *label_circleSecond;
    UILabel *label_circleThird;
    UILabel *label_circleFour;
    
    int record_choseMonthSYuexi;//记录选中的月份对应的利息

}

@property (nonatomic,strong) UIView *view_topCircleBG;

@property (nonatomic,strong) UIView *view_middleChoseMoneyAndTimeBG;

@property (nonatomic,strong) UIView *view_bottomCoductionBG;

@property(nonatomic,strong) PickViewSelect *pickView;

@property (nonatomic,strong) NSMutableArray *mutableArray_month;
@property (nonatomic,strong) NSMutableArray *mutableArray_yuexi;
@property (nonatomic,strong) NSMutableArray *mutableArray_dataALL;

@property (nonatomic,strong) NSMutableDictionary *mutableDic_bagInfo;//帮我贷所有信息一个桥梁

@end

@implementation HelpMeBagApplyNowVC

- (NSMutableArray *)mutableArray_month
{
    if (_mutableArray_month==nil) {
        _mutableArray_month = [[NSMutableArray alloc] init];
    }
    return _mutableArray_month;
}

- (NSMutableArray *)mutableArray_yuexi
{
    if (_mutableArray_yuexi==nil) {
        _mutableArray_yuexi = [[NSMutableArray alloc] init];
    }
    return _mutableArray_yuexi;
}

- (NSMutableArray *)mutableArray_dataALL
{
    if (_mutableArray_dataALL==nil) {
        _mutableArray_dataALL = [[NSMutableArray alloc] init];
    }
    return _mutableArray_dataALL;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"帮我贷";
    self.view.backgroundColor = COLOR_EVERYVIEWBG;
    
    self.mutableDic_bagInfo = [[NSMutableDictionary alloc] init];
    
    [self.mutableDic_bagInfo setObject:[NSNumber numberWithFloat:0.08] forKey:@"poundage"];
    [self.mutableDic_bagInfo setObject:[NSNumber numberWithInt:3] forKey:@"loanbanks"];
    
    record_choseMonthSYuexi = 4;//初始化
    
    for (NSString *value in self.dic_limitmin) {
        
        NSString *str_yuexi = self.dic_limitmin[value];
        
        NSArray *array = [str_yuexi componentsSeparatedByString:@","]; //从字符A中分隔成2个元素的数组
        [self.mutableArray_dataALL addObject:str_yuexi];
        [self.mutableArray_yuexi addObject:array[1]];
        [self.mutableArray_month addObject:array[0]];
    }
    
    //布局
    [self initWithHelpMeBagApplyNowVCSubviews];
    
    [self initWithRightButtonSubviews];

}

#pragma mark --- right button
- (void) initWithRightButtonSubviews
{
    UIButton *button_rightChange = [UIButton buttonWithType:UIButtonTypeCustom];
    [button_rightChange setTitle:@"返回首页"  forState:UIControlStateNormal];
    [button_rightChange setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    button_rightChange.frame = CGRectMake(15, 0, 60, 30);
    [button_rightChange.titleLabel setFont:[UIFont systemFontOfSize:24*PROPORTION_WIDTH]];
    
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button_rightChange];
    self.navigationItem.rightBarButtonItem = menuButton;
    
    [button_rightChange addTarget:self action:@selector(backToHomeViewAction)
                 forControlEvents:UIControlEventTouchUpInside];
    
}

//返回到首页
- (void) backToHomeViewAction
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}


#pragma mark --- 创建视图页面 布局
- (void) initWithHelpMeBagApplyNowVCSubviews
{
    //top
    [self initWithTopCircleSubViews];
    
    //middle
    [self initWithmiddleChoseMoneyAndTimeBGSubViews];
    
    //bottom
    [self initWithBottomCoductionBGSubViews];
    
    //button
    button_applyNow = [[UIButton alloc] initWithFrame:CGRectMake(48*PROPORTION_WIDTH,kScreenHeight-93*PROPORTION_WIDTH-10,kScreenWidth - 48*PROPORTION_WIDTH*2, 93*PROPORTION_WIDTH)];
    [self.view addSubview:button_applyNow];
    
    button_applyNow.backgroundColor = COLOR_BUTTON;
    
    [button_applyNow setTitle:@"立即申请" forState:UIControlStateNormal];
    [button_applyNow setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button_applyNow.titleLabel setFont:[UIFont systemFontOfSize:Font_SIZE_18*2]];
    button_applyNow.layer.cornerRadius = 3.0;
    button_applyNow.clipsToBounds = YES;
    
    [button_applyNow addTarget:self action:@selector(ApplayNowAction) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark --- 创建头部 视图界面
- (void) initWithTopCircleSubViews
{
    //背景
    self.view_topCircleBG = [[UIView alloc] initWithFrame:CGRectMake(0,64, kScreenWidth,288*PROPORTION_WIDTH)];
    [self.view addSubview:self.view_topCircleBG];
    self.view_topCircleBG.backgroundColor = [UIColor whiteColor];
    
    //横线
    UIView *view_line  = [[UIView alloc] initWithFrame:CGRectMake(0,270*PROPORTION_WIDTH,kScreenWidth,20.0*PROPORTION_WIDTH)];
    [self.view_topCircleBG addSubview:view_line];
    view_line.backgroundColor = COLOR_EVERYVIEWBG;
    
    // 那个块块
    UIView *view_block  = [[UIView alloc] initWithFrame:CGRectMake((kScreenWidth-27*PROPORTION_WIDTH)/2,view_line.y,27*PROPORTION_WIDTH,view_line.height)];
    [self.view_topCircleBG addSubview:view_block];
    view_block.backgroundColor = [BaseViewController colorWithHexString:@"#009944"];
    
    //first circle
    label_circleFirst = [[UILabel alloc] initWithFrame:CGRectMake(77*PROPORTION_WIDTH, 44*PROPORTION_WIDTH, 100*PROPORTION_WIDTH, 100*PROPORTION_WIDTH)];
    [self.view_topCircleBG addSubview:label_circleFirst];
    label_circleFirst.text = @"0月\n贷款期限";
    label_circleFirst.textColor = [UIColor whiteColor];
    label_circleFirst.font = [UIFont systemFontOfSize:Font_SIZE_18];
    label_circleFirst.textAlignment = NSTextAlignmentCenter;
    label_circleFirst.backgroundColor = [BaseViewController colorWithHexString:@"#fc693d"];
    label_circleFirst.layer.cornerRadius = label_circleFirst.width/2;
    label_circleFirst.clipsToBounds = YES;
    label_circleFirst.numberOfLines = 2;

    //second circle
    label_circleSecond = [[UILabel alloc] initWithFrame:CGRectMake(249*PROPORTION_WIDTH, 72*PROPORTION_WIDTH, 146*PROPORTION_WIDTH, 146*PROPORTION_WIDTH)];
    [self.view_topCircleBG addSubview:label_circleSecond];
    label_circleSecond.text = @"0元\n总利息";
    label_circleSecond.textColor = [UIColor whiteColor];
    label_circleSecond.font = [UIFont systemFontOfSize:Font_SIZE_18];
    label_circleSecond.textAlignment = NSTextAlignmentCenter;
    label_circleSecond.backgroundColor = [BaseViewController colorWithHexString:@"#035b7f"];
    label_circleSecond.layer.cornerRadius = label_circleSecond.width/2;
    label_circleSecond.clipsToBounds = YES;
    label_circleSecond.numberOfLines = 2;
    
    //third circle
    label_circleThird = [[UILabel alloc] initWithFrame:CGRectMake(463*PROPORTION_WIDTH, 33*PROPORTION_WIDTH, 102*PROPORTION_WIDTH, 102*PROPORTION_WIDTH)];
    [self.view_topCircleBG addSubview:label_circleThird];
    label_circleThird.text = @"0元\n贷款金额";
    label_circleThird.textColor = [UIColor whiteColor];
    label_circleThird.font = [UIFont systemFontOfSize:Font_SIZE_18];
    label_circleThird.textAlignment = NSTextAlignmentCenter;
    label_circleThird.backgroundColor = [BaseViewController colorWithHexString:@"#5dc1cc"];
    label_circleThird.layer.cornerRadius = label_circleThird.width/2;
    label_circleThird.clipsToBounds = YES;
    label_circleThird.numberOfLines = 2;

    //four cirle
    label_circleFour = [[UILabel alloc] initWithFrame:CGRectMake(539*PROPORTION_WIDTH, 164*PROPORTION_WIDTH, 102*PROPORTION_WIDTH, 102*PROPORTION_WIDTH)];
    [self.view_topCircleBG addSubview:label_circleFour];
    label_circleFour.text = @"0元\n月还款";
    label_circleFour.textColor = [UIColor whiteColor];
    label_circleFour.font = [UIFont systemFontOfSize:Font_SIZE_18];
    label_circleFour.textAlignment = NSTextAlignmentCenter;
    label_circleFour.backgroundColor = [BaseViewController colorWithHexString:@"#c2e8e4"];
    label_circleFour.layer.cornerRadius = label_circleFour.width/2;
    label_circleFour.clipsToBounds = YES;
    label_circleFour.numberOfLines = 2;
}

#pragma mark --- 创建头部 视图界面
- (void) initWithmiddleChoseMoneyAndTimeBGSubViews
{
    //背景
    self.view_middleChoseMoneyAndTimeBG = [[UIView alloc] initWithFrame:CGRectMake(0,self.view_topCircleBG.bottom, kScreenWidth,200*PROPORTION_WIDTH)];
    [self.view addSubview:self.view_middleChoseMoneyAndTimeBG];
    self.view_middleChoseMoneyAndTimeBG.backgroundColor = [UIColor whiteColor];
    
    //银行名字
    UILabel *label_bankName = [[UILabel alloc] initWithFrame:CGRectMake(77*PROPORTION_WIDTH, 37*PROPORTION_WIDTH, 138*PROPORTION_WIDTH, 22*PROPORTION_WIDTH)];
    [self.view_middleChoseMoneyAndTimeBG addSubview:label_bankName];
    label_bankName.textColor = COLOR_TEXT_132;
    label_bankName.font = [UIFont systemFontOfSize:Font_SIZE_30];
    label_bankName.text = @"北京银行-";
    
    //who bag
    UILabel *label_Person = [[UILabel alloc] initWithFrame:CGRectMake(label_bankName.right, label_bankName.y, 330*PROPORTION_WIDTH, 22*PROPORTION_WIDTH)];
    [self.view_middleChoseMoneyAndTimeBG addSubview:label_Person];
    label_Person.textColor = COLOR_TEXT_0;
    label_Person.font = [UIFont systemFontOfSize:Font_SIZE_30];
    label_Person.text = @"按揭房、抵押房专案";
    
    //横线 first
    UIView *view_lineFirst  = [[UIView alloc] initWithFrame:CGRectMake(0,101*PROPORTION_WIDTH,kScreenWidth,5.0*PROPORTION_WIDTH)];
    [self.view_middleChoseMoneyAndTimeBG addSubview:view_lineFirst];
    view_lineFirst.backgroundColor = COLOR_EVERYVIEWBG;

    //横线 second
    UIView *view_line  = [[UIView alloc] initWithFrame:CGRectMake(0,190*PROPORTION_WIDTH,kScreenWidth,10.0*PROPORTION_WIDTH)];
    [self.view_middleChoseMoneyAndTimeBG addSubview:view_line];
    view_line.backgroundColor = COLOR_EVERYVIEWBG;
    
    //money time
    [self choseMoenyAndTimeSubviews];
}

//选择金额 期限 布局
- (void) choseMoenyAndTimeSubviews
{
    //money title
    UILabel *label_money = [[UILabel alloc] initWithFrame:CGRectMake(77*PROPORTION_WIDTH, 136*PROPORTION_WIDTH, 63*PROPORTION_WIDTH, 28*PROPORTION_WIDTH)];
    [self.view_middleChoseMoneyAndTimeBG addSubview:label_money];
    label_money.text = @"金额 :";
    label_money.textColor = COLOR_TEXT_132;
    label_money.font = [UIFont systemFontOfSize:Font_SIZE_22];
    
    //button
    textField_money = [[UITextField alloc] initWithFrame:CGRectMake(label_money.right+5*PROPORTION_WIDTH, 131*PROPORTION_WIDTH, 137*PROPORTION_WIDTH, 36*PROPORTION_WIDTH)];
    [self.view_middleChoseMoneyAndTimeBG addSubview:textField_money];
    textField_money.layer.borderWidth = 1.0;
    textField_money.layer.borderColor = [BaseViewController colorWithHexString:@"#c4c4c4"].CGColor;
    textField_money.delegate = self;
    textField_money.keyboardType = UIKeyboardTypeNumberPad;
    textField_money.font = [UIFont systemFontOfSize:Font_SIZE_18];
    
     [textField_money addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    //imageView  title
    UILabel *imageView_money = [[UILabel alloc] initWithFrame:CGRectMake(textField_money.right, textField_money.y, textField_money.height,textField_money.height)];
    [self.view_middleChoseMoneyAndTimeBG addSubview:imageView_money];
    imageView_money.text = @"元";
    imageView_money.textColor = COLOT_TEXTGRAY;
    imageView_money.font = [UIFont systemFontOfSize:Font_SIZE_18];
    
    //time title
    UILabel *label_time = [[UILabel alloc] initWithFrame:CGRectMake(161*PROPORTION_WIDTH+textField_money.right, 136*PROPORTION_WIDTH, 63*PROPORTION_WIDTH, 28*PROPORTION_WIDTH)];
    [self.view_middleChoseMoneyAndTimeBG addSubview:label_time];
    label_time.text = @"期限 :";
    label_time.textColor = COLOR_TEXT_132;
    label_time.font = [UIFont systemFontOfSize:Font_SIZE_22];
    
    //  time  button
    button_time = [[UIButton alloc] initWithFrame:CGRectMake(label_time.right+5*PROPORTION_WIDTH, textField_money.y, textField_money.width, textField_money.height)];
    [self.view_middleChoseMoneyAndTimeBG addSubview:button_time];
    button_time.layer.borderWidth = 1.0;
    button_time.layer.borderColor = [BaseViewController colorWithHexString:@"#c4c4c4"].CGColor;
    
    [button_time setTitle:self.mutableArray_month[0] forState:UIControlStateNormal];
    [self.mutableDic_bagInfo setObject:button_time.titleLabel.text forKey:@"period"];

    [button_time setTitleColor:COLOR_TEXT_132 forState:UIControlStateNormal];
    [button_time.titleLabel setFont:[UIFont systemFontOfSize:Font_SIZE_22]];
    
    button_time.tag = 11;
    [button_time addTarget:self action:@selector(choseMoenyAndTimePickerViewAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //imageView
    UIImageView *imageView_time = [[UIImageView alloc] initWithFrame:CGRectMake(textField_money.width-imageView_money.height, 0, imageView_money.width, textField_money.height)];
    [button_time addSubview:imageView_time];
    imageView_time.image = [UIImage imageNamed:@"arrowdown"];

}

#pragma mark --- 创建申请条件页面
- (void) initWithBottomCoductionBGSubViews
{
    //背景
    self.view_bottomCoductionBG = [[UIView alloc] initWithFrame:CGRectMake(0,self.view_middleChoseMoneyAndTimeBG.bottom, kScreenWidth,490*PROPORTION_WIDTH)];
    [self.view addSubview:self.view_bottomCoductionBG];
    self.view_bottomCoductionBG.backgroundColor = [UIColor whiteColor];
    
    //服务
    UIWebView *webView_discount = [[UIWebView alloc] initWithFrame:CGRectMake(10,3, self.view_bottomCoductionBG.width-20,self.view_bottomCoductionBG.height-6)];
    [self.view_bottomCoductionBG addSubview:webView_discount];
    
    UIFont *fontSize = [UIFont systemFontOfSize:Font_SIZE_24];
    
    //控制webView的 字体大小
    NSString *BookStr = [NSString stringWithFormat:@"<html> \n"
                         "<head> \n"
                         "<style type=\"text/css\"> \n"
                         "body {margin:0;font-size: %@;color: %@;}\n"
                         "</style> \n"
                         "</head> \n"
                         "<body>%@</body> \n"
                         "</html>",fontSize,COLOT_TEXTGRAY,self.model_baglist.info];
    
    [webView_discount loadHTMLString:BookStr baseURL:nil];
    
    //此处是处理网页黑条条的状况
    webView_discount.opaque = NO;
    [webView_discount setScalesPageToFit:YES];
    webView_discount.backgroundColor = [UIColor clearColor];

}

#pragma mark -- picker View
- (void) choseMoenyAndTimePickerViewAction:(UIButton *)sender
{
    [textField_money resignFirstResponder];//取消键盘第一响应
    
    _pickView =[[PickViewSelect alloc]initWithDataArr:self.mutableArray_month andRowIndex:(int)sender.tag];//0在此处无意义
    _pickView.delegate = self;
    [self.view addSubview:_pickView];
}

#pragma mark -- picker delegate
- (void) pickViewdelegateWith:(NSString *)messStr andRow:(int)rowIndex andSelectecRow:(int)selectedRow
{
    
    [button_time setTitle:messStr forState:UIControlStateNormal];
    
    record_choseMonthSYuexi = selectedRow;
    
    if (textField_money.text!=0) {
        //贷款总利息：贷款金额*利息%*期数   每个月利息：贷款金额*利息%
        //总利息=贷款金额*月利率*贷款期数    月还款额=贷款金额/贷款期数+贷款金额*月利率
        CGFloat loanAllLixi,oneMonthLixi;
        if (record_choseMonthSYuexi!=4) {
            
            loanAllLixi = [textField_money.text intValue]*[self.mutableArray_yuexi[record_choseMonthSYuexi] floatValue]*[button_time.titleLabel.text intValue];
            
            oneMonthLixi = [textField_money.text intValue]*[self.mutableArray_yuexi[record_choseMonthSYuexi] floatValue] + [textField_money.text intValue]/[button_time.titleLabel.text intValue];
        }
        else
        {
            loanAllLixi = [textField_money.text intValue]*[self.mutableArray_yuexi[0] floatValue]*[button_time.titleLabel.text intValue];
            
            oneMonthLixi = [textField_money.text intValue]*[self.mutableArray_yuexi[0] floatValue];
        }
        
        label_circleFirst.text = [NSString stringWithFormat:@"%@月\n贷款期限",button_time.titleLabel.text];
        label_circleSecond.text = [NSString stringWithFormat:@"%.2f元\n总利息",loanAllLixi/100];
        label_circleThird.text = [NSString stringWithFormat:@"%@元\n贷款金额",textField_money.text];
        label_circleFour.text = [NSString stringWithFormat:@"%.2f元\n月还款",oneMonthLixi/100];
    }

    [self.mutableDic_bagInfo setObject:button_time.titleLabel.text forKey:@"period"];

}

/**
 * 限制 金额的长度
 */
- (void) textFieldDidChange:(UITextField *)textField
{
    if (textField==textField_money) {
        
       [self.mutableDic_bagInfo setObject:textField_money.text forKey:@"loanamount"];
        
        if (textField_money.text.length>7) {
     
            textField.text = [textField.text substringToIndex:8];
        }
    }
}

- (void) textFieldDidEndEditing:(UITextField *)textField
{
    if (textField_money==textField) {
        //贷款总利息：贷款金额*利息%*期数   每个月利息：贷款金额*利息%
        //总利息=贷款金额*月利率*贷款期数    月还款额=贷款金额/贷款期数+贷款金额*月利率
        CGFloat loanAllLixi,oneMonthLixi;
        if (record_choseMonthSYuexi!=4) {
        
          loanAllLixi = [textField.text intValue]*[self.mutableArray_yuexi[record_choseMonthSYuexi] floatValue]*[button_time.titleLabel.text intValue];
         
          oneMonthLixi = [textField.text intValue]*[self.mutableArray_yuexi[record_choseMonthSYuexi] floatValue] + [textField_money.text intValue]/[button_time.titleLabel.text intValue];
        }
        else
        {
            loanAllLixi = [textField.text intValue]*[self.mutableArray_yuexi[0] floatValue]*[button_time.titleLabel.text intValue];
            
            oneMonthLixi = [textField.text intValue]*[self.mutableArray_yuexi[0] floatValue];
        }
        
        label_circleFirst.text = [NSString stringWithFormat:@"%@月\n贷款期限",button_time.titleLabel.text];
        label_circleSecond.text = [NSString stringWithFormat:@"%.2f元\n总利息",loanAllLixi/100];
        label_circleThird.text = [NSString stringWithFormat:@"%@元\n贷款金额",textField.text];
        label_circleFour.text = [NSString stringWithFormat:@"%.2f元\n月还款",oneMonthLixi/100];
    }
}

#pragma mark -- 立即申请
- (void) ApplayNowAction
{
    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"memberid"]) {
        
        
        LoginViewController *loginView = [[LoginViewController alloc] init];
        loginView.fromeWhere = @"bagApplyNow";
        [self.navigationController pushViewController:loginView animated:YES];
    }
    else
    {
        if (textField_money.text.length>0&&button_time.titleLabel.text){
            
            //添加字段
            [self.mutableDic_bagInfo setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"mobile"] forKey:@"mobile"];

            
            HelpMeBagApplyFirstStepVC *firstStepView = [[HelpMeBagApplyFirstStepVC alloc] init];
            
            firstStepView.mutableDic_pathBagInfo = self.mutableDic_bagInfo;
            
            [self.navigationController pushViewController:firstStepView animated:YES];
        }
        else
        {
            [UIView showAlertMsg:@"请完善信息"];
        }
    }
}

- (void) touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view_bottomCoductionBG endEditing:YES];
    [self.view_middleChoseMoneyAndTimeBG endEditing:YES];
    [self.view_bottomCoductionBG endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
